<?php
/* Reminder: always indent with 4 spaces (no tabs). */
// +---------------------------------------------------------------------------+
// |  記事一覧                                       |
// +---------------------------------------------------------------------------+
// $Id: public_html/assist/articles.php
define ('THIS_SCRIPT', 'assist/articles.php');
//define ('THIS_SCRIPT', 'assist/articlestest.php');
define ('NEXT_SCRIPT', 'article.php');
//define ('THIS_SCRIPT', 'articletest.php');
//20110728 tsuchitani AT ivywe DOT co DOT jp http://www.ivywe.co.jp/

require_once ('../lib-common.php');

// +---------------------------------------------------------------------------+
// |   記事一覧表示
// | 書式  assist_articles($id,$template,"yes","page",$perpage)
// | 書式  assist_articles($p['category_id'],$p['thtml'],$p['nohitmsg']
// | 書式                    ,"",$p['perpage'],$p['order'],$p['code']
// +---------------------------------------------------------------------------+
// | 引数 $autotag:notautotagの時ヘッダを表示しない
// | 引数 $topic_id:話題id
	// | 引数 $category_id:カテゴリid
// | 引数 $template:使用するテンプレートのフォルダ名
// | 引数 $nohitmsg      :"yes"の時nohitメッセージ表示
// | 引数 $m             :"page"の時ヘッダ表示
// | 引数 $perpage       :1pageあたりの件数、0の時改ページなし
// | 引数 $order         :順序
	// | 引数 $code          :

// +---------------------------------------------------------------------------+
// | public_html/articles.php および　自動タグで使用
// | ヘッダはコンフィギュレーション設定により表示されない場合があります
// | idもcodeも指定されない場合は、全話
// +---------------------------------------------------------------------------+
// | 戻値 nomal:articles list
// +---------------------------------------------------------------------------+
function assist_articles(
    $autotag
    ,$topics
    ,$template=null
    ,$nohitmsg=""
    ,$perpage =0
    ,$page=null
    ,$order=null
)
{

    $pi_name="assist";

	global $_CONF;
	global $_TABLES;
		//global $_ASSIST_CONF;
	global $LANG_ASSIST;
		//global $LANG_ASSIST_ADMIN;


    //-----引数チェック
    if (is_null($page)){
        $page=1;
    }
	
    if ($perpage===0 OR is_null($perpage)){
        $perpage=$_CONF['limitnews'];
    }

    //-----ページ
    if (!isset($page) OR $page == 0) {
        $page = 1;
	}

    if (is_null($template) or ($template==="")){
        $template="default";
    }


    //-----

    $sql = "SELECT ".LB;
    $sql.=" s.title".LB;
    $sql.=" , UNIX_TIMESTAMP(s.date) AS day".LB;
    $sql.=" , t.topic".LB;
    $sql.=" , s.tid".LB;
    $sql.=" , s.sid".LB;

    $sql.=" FROM ".LB;
    $sql.=" {$_TABLES['stories']} AS s".LB;
    $sql.=" ,{$_TABLES['topics']} AS t".LB;

    $sql.=" WHERE".LB;
    $sql.="  s.tid = t.tid".LB;
    $sql.=" AND t.archive_flag=0 ".LB;
    $sql.=" AND s.draft_flag=0 ".LB;
    $sql.=" AND date <= NOW() ".LB;

    if ($topics!=="'ALL'" ){
        $sql.=" AND s.tid IN ($topics)";
	}
	
    $sql .= COM_getTopicSql ('AND', 0, 's');
    $sql .= COM_getPermSql ('AND', 0, 2, 's' );
    $sql .= COM_getLangSQL ('sid', 'AND');

    $sql.=" ORDER BY date DESC";


    $result = DB_query ($sql);
    $cnt = DB_numRows ($result);

    $pages = 0;
     if ($perpage > 0) {
        $pages = ceil($cnt / $perpage);
    }

    //ヘッダ、左ブロック
    if ($autotag==="notautotag"){
        if ($page > 1) {
            $page_title = sprintf ('%s (%d)', $topic_name, $page);
        } else {
            $page_title = sprintf ('%s ', $topic_name);
        }
        $headercode="<title>".$_CONF['site_name']." - ".$page_title ."</title>";
	    $retval .= COM_siteHeader('menu', $page_title);

    }
	
	$tmplfld=databox_templatePath('articles',$template,$pi_name);
	
    $templates = new Template($tmplfld);
    $templates->set_file (array (
        'list' => 'list.thtml',
        'nav'   => 'navigation.thtml',
        'row'   => 'row.thtml',
		'col'   => "col.thtml",
	    'grp'   => "grp.thtml",
        'pagenav'  => 'pagenavigation.thtml'
        ));


    //
    $templates->set_var ('site_url',$_CONF['site_url']);
    $templates->set_var ('this_script',THIS_SCRIPT);

    //bread
    $templates->set_var ('home',$LANG_ASSIST['home']);

    $templates->set_var ('topic_name',$topic_name);

    //page
    $offset = ($page - 1) * $perpage;
    $templates->set_var ('cnt', $cnt);
    $lin1=$offset+1;
    $lin2=$lin1+$perpage - 1;
    if ($lin2>$cnt){
        $lin2=$cnt;
    }

    //summary navigation
    $templates->set_var ('lang_view', $LANG_ASSIST['view']);
    $templates->set_var ('lin', $lin1."-".($lin2));
    $templates->set_var ('cnt', $cnt);

//    $templates->set_var ('lang_name', $LANG_DATABOX_ADMIN['name']);
//    $templates->set_var ('lang_code', $LANG_DATABOX_ADMIN['code']);
//    $templates->set_var ('lang_id', $LANG_DATABOX_ADMIN['id']);

    $sql .= " LIMIT $offset, $perpage";

    $result = DB_query ($sql);
    $numrows = DB_numRows ($result);

    if ($numrows > 0) {
        for ($i = 0; $i < $numrows; $i++) {
            $A = DB_fetchArray ($result);

            $title=COM_applyFilter($A['title']);
            $description=COM_applyFilter($A['description']);
            $code=COM_applyFilter($A['code']);
            $id=COM_applyFilter($A['id']);
            $datefield=COM_applyFilter($A['datefield']);
            $value=COM_applyFilter($A['value']);

            $url=$_CONF['site_url'] . "/databox/data.php";
            $url.="?";
            $url.="&id=".$A['id'];
            $url = COM_buildUrl( $url );
            $link= COM_createLink($title, $url);


            $templates->set_var ('data_link', $link);
            $templates->set_var ('data_title', $title);
            $templates->set_var ('data_code', $code);
            $templates->set_var ('data_description', $description);
            $templates->set_var ('data_id', $id);
            $templates->set_var ('data_url', $url);
            $templates->set_var ('data_datefield', $datefield);
            $templates->set_var ('data_value', $value);

            //=====
            $templates->parse ('col_var', 'col', true);
            $templates->parse ('row_var', 'row', true);

            $templates->set_var ('col_var', '');

        }

        //------------
        //-----navigation
        $url = $_CONF['site_url']  . '/';
        if ($autotag==="notautotag"){
            $url.=THIS_SCRIPT;
        }else{
            $url.="assist/articles.php";
        }
        $url .= "&tid=".$topic_id;
        $url .= "&template=".$template;
        $url .= "&order=";
        //-----order navigation
        databox_order ($url, $templates, $order) ;
        //-----page navigation
        $url .= $order;
        $templates->set_var ('page_navigation',
                COM_printPageNavigation ($url, $page, $pages));
        if ($order==="random"){
            $templates->set_var ( 'pagenavinone', 'style="display:none;"' );
        }else{
            $templates->set_var ( 'pagenavinone', '' );
        }
        //------------
        $templates->parse ('nav_var', 'nav', true);

        $templates->set_var ('msg', "");

        $templates->parse ('output', 'list');

        $content = $templates->finish ($templates->get_var ('output'));
        $retval .=$content;

    }else{
        if ($nohitmsg==="yes"){
            $templates->set_var ('msg', $LANG_DATABOX["nohit"]);
            //@@@@@@$templates->set_var ('search_link', $LANG_DATABOX['search_link']);//@@@@@
            $templates->parse ('output', 'list');
            $content = $templates->finish ($templates->get_var ('output'));
            $retval .=$content;
        }
    }

    $retval =PLG_replacetags ($retval);

    return $retval;
}
// +---------------------------------------------------------------------------+
// MAIN
// +---------------------------------------------------------------------------+
//############################
$pi_name    = 'assist';
//############################

//引数
if ($_CONF['url_rewrite']){
    COM_setArgNames(array('tid','template'));
    $tid=COM_applyFilter(COM_getArgument('tid'));
	$template=COM_applyFilter(COM_getArgument('template'));
    $page = COM_applyFilter($_REQUEST['page'],true);
    $order = COM_applyFilter($_REQUEST['order']);
}else{
    $tid = COM_applyFilter($_REQUEST['tid']);
    $template = COM_applyFilter($_REQUEST['template']);
    $page = COM_applyFilter($_REQUEST['page'],true);
    $order = COM_applyFilter($_REQUEST['order']);
}
if ($tid<>""){
	$topics[]=$tid;
}else{
	$topics="";
}

$display = '';
$page_title=$LANG_ASSIST['articles'];

$display .= COM_siteHeader('menu', $page_title);


$display .= assist_articles("notautotag",$topics,$template,"yes",$perpage,$page,$order);

$display .= COM_siteFooter();

//---

COM_output($display);

?>